package com.webservice.cliente;

import java.io.IOException;

import org.json.JSONException;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.content.SharedPreferences;
import android.util.Log;


@SuppressWarnings("deprecation")
/**
 * 
 */
public class ClienteWsPaciente {
	private static String METHOD_NAME ;
	private static String NAMESPACE ;
	private static String URL ="http://192.168.1.10:8080/ServicioWebSoap/services/BabyMonitorWS";

	/**
	 * 
	 * @param methodName
	 */

	public static void setAttribute(String methodName) {

		// TODO Auto-generated constructor stub

		METHOD_NAME = methodName;
		NAMESPACE = "Direccion";
		URL = "URL";
	}
	/**
	 * 
	 * @param methodName
	 * @return
	 */
	public static String Servicio(String methodName){
		String METHOD_NAME = methodName;
		String NAMESPACE = "http://ws.serviciosoap.pds.luisburgos.com";
		String URL = "http://londres.ceisufro.cl:8080/SGMAS/services/Paciente";

		return Servicio(METHOD_NAME, NAMESPACE, URL);
	}
	/**
	 * 
	 * @param method_name
	 * @param namespace
	 * @param url
	 * @return
	 */
	public static String Servicio(String method_name,String namespace, String url){
		
		
		String resultadoJSON="";
		String METHOD_NAME = "getPregunta";
		String SOAP_ACTION = NAMESPACE +"/" +METHOD_NAME;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		soapEnvelope.dotNet = true;
		soapEnvelope.implicitTypes =true;
		soapEnvelope.setOutputSoapObject(request);
		
		HttpTransportSE aht = new HttpTransportSE(URL);
		
		try {
			aht.call(SOAP_ACTION, soapEnvelope);
			SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
			resultadoJSON = resultString.toString();
			
			
		} catch (IOException e) {
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		}
		
		return resultadoJSON;
	}
		
		
		
		
		
		
		


	/**
	 * 
	 * @param method_name
	 * @param rut
	 * @return
	 */
	public static String ServicioGetPregunta(String method_name,String rut){
		
		
		
		
		
		String resultadoJSON="";
    	String METHOD_NAME = "getPregunta";
    	String SOAP_ACTION = NAMESPACE +"/" +METHOD_NAME;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		soapEnvelope.dotNet = true;
		soapEnvelope.implicitTypes =true;
		soapEnvelope.setOutputSoapObject(request);

		HttpTransportSE aht = new HttpTransportSE(URL);
		Log.d("Cliente SOAP", "Iniciando Consulta");
		try {
			aht.call(SOAP_ACTION, soapEnvelope);
			SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
			resultadoJSON = resultString.toString();
			Log.d("Cliente SOAP", "Finalizando Consulta");
			
			
		} catch (IOException e) {
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		}
    
		return resultadoJSON;
	}

		

	/**
	 * Retorna falso si el rut no existe y verdadero si existe.
	 * @return Boolean
	 */
	public static Boolean rutWebservice(String rut, SharedPreferences settings){

		JSONObject jObject;
		try {

			String datosJsonString = ClienteWsPaciente.ServicioGetPregunta("traePregunta",rut);

			if(datosJsonString != null){
				//pasa el String a objeto JSON
				jObject = new JSONObject(datosJsonString);				

				/**
				 * se obtienen los valores del json.
				 */

				SharedPreferences.Editor editor = settings.edit();
				editor.putString("run",jObject.getString("run") );
				editor.putString("nombre", jObject.getString("nombre"));
				editor.putString("pregunda", jObject.getString("pregunta"));
				
				// Commit the edits!
				editor.commit();

				return true;

			}else{
				return false;
			}
		}
		catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();  
			return false;
		} 

	}
	
}
